package com.jie.deletefile.DongtaiVp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jie.deletefile.R;

public class HsActivity extends AppCompatActivity {

    private LinearLayout hs_ll;
    private RecyclerView h_rv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cvp);
        initView();
        initrv();
    }

    private void initrv() {
        h_rv = (RecyclerView) findViewById(R.id.h_rv);
        h_rv.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false));
        h_rv.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
        h_rv.setAdapter(new MyAdapter());
    }

    private void initView() {
        hs_ll = (LinearLayout) findViewById(R.id.hs_ll);
        for (int i = 0;i<10;i++) {
            View view = LayoutInflater.from(this).inflate(R.layout.hs_item, null);
            TextView tv = (TextView) view.findViewById(R.id.hs_tv);
            Button btn = (Button) view.findViewById(R.id.hs_btn);
            tv.setText("条目" + i);
            btn.setText("button" + i);
            final int finalI = i;
            tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(HsActivity.this, "条目"+ finalI, Toast.LENGTH_SHORT).show();
                }
            });
            final int finalI1 = i;
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(HsActivity.this, "btn"+ finalI1, Toast.LENGTH_SHORT).show();
                }
            });
            hs_ll.addView(view);

        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(HsActivity.this).inflate(R.layout.hs_item, parent, false));

            return holder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.tv.setText("条目" + position);
            holder.btn.setText("按钮"+position);
        }

        @Override
        public int getItemCount() {
            return 10;
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
             TextView tv;
             Button btn;


            public MyViewHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.hs_tv);
                btn = (Button) itemView.findViewById(R.id.hs_btn);
            }
        }
    }
}
